﻿using System;
using System.Collections.Generic;

namespace Microsoft.SharePoint.Client
{

    public static class Extensions
    {

        #region View, ViewCollection

        public static View FindDefaultView(this ViewCollection extendee)
        {
            return FindView(extendee, new Predicate<View>(IsDefaultViewPredicate));
        }

        public static View FindView(this ViewCollection extendee, Predicate<View> predicate)
        {
            return Find<View>(extendee, predicate);
        }

        private static bool IsDefaultViewPredicate(View view)
        {
            return view.DefaultView;
        }

        #endregion

        #region Field, FieldCollection

        public static Field FindField(this FieldCollection extendee, Predicate<Field> predicate)
        {
            return Find<Field>(extendee, predicate);
        }

        #endregion

        #region Generic

        public static T Find<T>(this IEnumerable<T> extendee, Predicate<T> predicate)
        {
            foreach (var i in extendee)
            {
                if (predicate(i))
                {
                    return i;
                }
            }
            return default(T);
        }

        #endregion

    }

}